package com.ruibang.glass.quality.service.impl;

import com.ruibang.glass.quality.constant.Constants;
import com.ruibang.glass.quality.constant.QualityErrorCode;
import com.ruibang.glass.quality.domain.req.ExternalDefectReq;
import com.ruibang.glass.quality.domain.resp.ExternalDefectOverlayMap;
import com.ruibang.glass.quality.service.ExternalDefectService;
import com.ruibang.glass.quality.service.TotalEquDataService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2023-12-26  09:10
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class ExternalDefectServiceImpl implements ExternalDefectService {

    @Autowired
    private TotalEquDataService totalEquDataService;

    /**
     * @Description: 外部缺陷叠加图
     * @version v1.0
     * @author songJinKang
     * @date 2023-12-26 9:11
     */
    @Override
    public ResultBody overlayMap(ExternalDefectReq externalDefectReq) {
        if (externalDefectReq == null) {
            return ResultBody.fail(QualityErrorCode.PARAM_ERROR.getCode(), QualityErrorCode.PARAM_ERROR.getMsg());
        }
        //默认查询1天数
        if (externalDefectReq.getStartTime() == null || externalDefectReq.getEndTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            externalDefectReq.setStartTime(now.minusDays(1));
            externalDefectReq.setEndTime(now);
        }
        //检查机对应关系
        String isF = "CM1".equals(externalDefectReq.getCheckMachineCode()) ? Constants.ONE_STR : Constants.ZERO_STR;
        externalDefectReq.setCheckMachineCode(isF);
        //查询数据
        List<ExternalDefectOverlayMap> externalDefectOverlayMap = totalEquDataService.queryExternalDefectOverlayMap(externalDefectReq);
        Map<String, List<ExternalDefectOverlayMap>> dataMap = new HashMap<>(2);
        if (Constants.ONE_STR.equals(externalDefectReq.getCheckMachineCode())) {
            if (CollectionUtils.isNotEmpty(externalDefectOverlayMap)) {
                dataMap.put("A", externalDefectOverlayMap.stream().filter(e -> e.getTiltedDiv() < 3).collect(Collectors.toList()));
                dataMap.put("B", externalDefectOverlayMap.stream().filter(e -> e.getTiltedDiv() >= 3).collect(Collectors.toList()));
            }
            return ResultBody.success(dataMap);
        }
        return ResultBody.success(externalDefectOverlayMap);
    }

}
